
FUNCTION WeekIndex RETURNS INTEGER (INPUT curDate AS DATE) FORWARD.
                                                       
                                                       
DEFINE VARIABLE i AS INTEGER.

                                                       
DISP weekindex(DATE(2,1,2007)).

FUNCTION WeekIndex RETURNS INTEGER (INPUT curDate AS DATE) :

    DEFINE VARIABLE idx AS INTEGER.

    DEFINE VARIABLE finYear AS INTEGER.

    DEFINE VARIABLE finDate AS DATE.

    IF curDate >= DATE(4, 1, YEAR(curDate)) THEN
        finYear = YEAR(curDate).
    ELSE
        finYear = YEAR(curDate) - 1.
        
    finDate = DATE(4, 1, finYear).

    IF WEEKDAY(finDate) = 1 THEN
        idx = TRUNCATE((curDate - finDate) / 7, 0) + 1.
    ELSE
        idx = TRUNCATE((curDate - finDate + WEEKDAY(finDate) - 1) / 7, 0).

    RETURN idx.

END.

